package io.kubesphere.devops.controller;

import io.kubesphere.devops.service.MergeManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p> UserMergingController 合并请求
 *
 * @author gaochuan
 * @version 0.1.0
 * @date 2021/11/16 14:11
 * @since 2021/11/16
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserMergingController {

    @Autowired
    private MergeManageService mergeManageService;

    /**
     * 账户合并请求
     * @param mergeRequestDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/merge")
    @ResponseBody
    public OperationResponse merge(@RequestBody MergeRequestDTO mergeRequestDTO) throws Exception {
        //TODO 判断是否业务处于未完全合并结束的状态,可以进行补偿
        return this.mergeCompensation(mergeRequestDTO);

    }

    /**
     * 账户合并正向补偿请求
     * @param mergeRequestDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/merge/compensation")
    @ResponseBody
    public OperationResponse mergeCompensation(@RequestBody MergeRequestDTO mergeRequestDTO) throws Exception {
        log.info("收到用户合并请求,请求详情:{}", mergeRequestDTO.toString());
        return mergeManageService.merge(mergeRequestDTO);
    }

    /**
     * 账户合并逆向撤销合并请求
     * @param xid
     * @return
     * @throws Exception
     */
    @PostMapping("/merged/revert")
    @ResponseBody
    public OperationResponse mergeRevert(@RequestParam String xid) throws Exception {
        return mergeManageService.phaseTwoRollback(xid);
    }
}
